package com.hs.srm.admin.modules.notice.controller;

import com.hs.srm.admin.modules.notice.entity.NoticeDefinitionManageEntity;
import com.hs.srm.admin.modules.notice.entity.NoticeRoleEntity;
import com.hs.srm.admin.modules.notice.service.NoticeDefinitionManageService;
import com.hs.srm.admin.modules.notice.service.NoticeRoleService;
import com.hs.srm.base.common.annotation.SysLog;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 通知管理，将通知类型与用户角色关联，发送通知消息通过角色查找用户
 *
 * @author ych
 * @since 2020-04-02 10:15:44
 */
@RestController
@RequestMapping("/noticeDefinition")
public class NoticeDefinitionManageController {

    @Autowired
    private NoticeDefinitionManageService noticeDefinitionService;

    @Autowired
    private NoticeRoleService noticeRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = noticeDefinitionService.queryPage(params);
        return R.ok(page);
    }

    /**
     * 保存通知定义
     *
     * @param noticeDefinitionEntity
     * @return
     */
    @SysLog("保存通知定义")
    @PostMapping("/saveDefinition")
    public R saveDefinition(@RequestBody NoticeDefinitionManageEntity noticeDefinitionEntity) {
        return noticeDefinitionService.saveDefinition(noticeDefinitionEntity);
    }

    /**
     * 删除通知类型
     *
     * @param pkIds
     * @return
     */
    @SysLog("删除通知类型")
    @PostMapping("/delete")
    public R delete(@RequestParam(value = "pkIds[]") String[] pkIds) {
        //先删除关联的数据
        Map<String, Object> map = new HashMap<>();
        Stream.of(pkIds).forEach(noticeId -> {
            map.put("notice_id", noticeId);
            noticeRoleService.deleteByMap(map);
        });

        //根据id删除数据
        noticeDefinitionService.deleteBatchIds(Arrays.asList(pkIds));
        return R.ok();
    }

    /**
     * 跳转到编辑页面
     *
     * @param pkId
     * @return
     */
    @RequestMapping("/toEdit/{pkId}")
    public ModelAndView toEdit(@PathVariable("pkId") String pkId) {
        NoticeDefinitionManageEntity entity = noticeDefinitionService.selectById(pkId);
        ModelAndView modelAndView = new ModelAndView("modules/sys/notice/notice_edit");
        modelAndView.addObject("entity", entity);
        return modelAndView;
    }

    /**
     * 获取关联的角色id
     *
     * @param pkId 通知类型id
     * @return
     */
    @GetMapping("/getRoleByNoticeId")
    public R getRoleByNoticeId(String pkId) {
        Map<String, Object> map = new HashMap<>();
        map.put("notice_id", pkId);
        //获取关联的角色id
        List<NoticeRoleEntity> entityList = noticeRoleService.selectByMap(map);
        List<Integer> roleIds = entityList.stream().map(NoticeRoleEntity::getRoleId).collect(Collectors.toList());
        return R.ok(roleIds);
    }
}